package com.ruoyi.web.controller.system;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.BusinessTrips;
import com.ruoyi.system.service.IBusinessTripsService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 出差申请Controller
 * 
 * @author ruoyi
 * @date 2025-03-21
 */
@RestController
@RequestMapping("/system/trips")
public class BusinessTripsController extends BaseController
{
    @Autowired
    private IBusinessTripsService businessTripsService;

    /**
     * 查询出差申请列表
     */
    @PreAuthorize("@ss.hasPermi('system:trips:approve') or @ss.hasPermi('system:tripsApply:list')")
    @GetMapping("/list")
    public TableDataInfo list(BusinessTrips businessTrips)
    {
        startPage();
        List<BusinessTrips> list = businessTripsService.selectBusinessTripsList(businessTrips);
        return getDataTable(list);
    }

    /**
     * 导出出差申请列表
     */
    @PreAuthorize("@ss.hasPermi('system:trips:export') or @ss.hasPermi('system:tripsApply:export')")
    @Log(title = "出差申请", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BusinessTrips businessTrips)
    {
        List<BusinessTrips> list = businessTripsService.selectBusinessTripsList(businessTrips);
        ExcelUtil<BusinessTrips> util = new ExcelUtil<BusinessTrips>(BusinessTrips.class);
        util.exportExcel(response, list, "出差申请数据");
    }

    /**
     * 获取出差申请详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:trips:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(businessTripsService.selectBusinessTripsById(id));
    }

    /**
     * 新增出差申请
     */
    @PreAuthorize("@ss.hasPermi('system:trips:add') or @ss.hasPermi('system:tripsApply:add')")
    @Log(title = "出差申请", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BusinessTrips businessTrips)
    {
        return toAjax(businessTripsService.insertBusinessTrips(businessTrips));
    }

    /**
     * 修改出差申请
     */
    @PreAuthorize("@ss.hasPermi('system:trips:edit') or @ss.hasPermi('system:tripsApply:edit')")
    @Log(title = "出差申请", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BusinessTrips businessTrips)
    {
        return toAjax(businessTripsService.updateBusinessTrips(businessTrips));
    }

    /**
     * 删除出差申请
     */
    @PreAuthorize("@ss.hasPermi('system:trips:remove') or @ss.hasPermi('system:tripsApply:remove')")
    @Log(title = "出差申请", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(businessTripsService.deleteBusinessTripsByIds(ids));
    }

    @PreAuthorize("@ss.hasPermi('system:trips:approve') or @ss.hasPermi('system:tripsApply:approve')")
    @Log(title = "出差申请", businessType = BusinessType.UPDATE)
    @RequestMapping("/aprrove")
    public AjaxResult approve(@RequestBody BusinessTrips businessTrips)
    {
        return toAjax(businessTripsService.approveBusinessTrips(businessTrips));
    }

    @PreAuthorize("@ss.hasPermi('system:trips:result') or @ss.hasPermi('system:tripsApply:result')")
    @PostMapping("/result/{id}")
    public AjaxResult selectResult(@PathVariable("id") Long id)
    {
        return success(businessTripsService.selectBusinessApplyResultById(id));
    }


    @PostMapping("/currentUser/{id}")
    public TableDataInfo selectCurrentUserBusinessTripsById(@PathVariable("id") Long id){
        startPage();
        List<BusinessTrips> list = businessTripsService.selectCurrentUserBusinessTripsById(id);
        return getDataTable(list);
    }

    @GetMapping("/detailed")
    public TableDataInfo selectDetailedBusinessTripsByIdAndInput(@RequestParam("id") Long id, @RequestParam(value = "status",required = false) String status, @RequestParam(value = "destination",required = false) String destination ){
        startPage();
        List<BusinessTrips> list = businessTripsService.selectDetailedBusinessTripsByIdAndInput(id,status,destination);
        return getDataTable(list);
    }
}
